package com.example.composecustomthemes

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import com.example.composecustomthemes.customtheme.CustomTheme
import com.example.composecustomthemes.ui.theme.ComposeCustomTheme
import com.example.composecustomthemes.ui.theme.darkColors

class CustomThemeActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            ComposeCustomTheme(darkColors = darkColors()) {
                ThemeUI()
            }
        }
    }
}

@Composable
fun ThemeUI() {
    Scaffold(
        containerColor = CustomTheme.colors.background,
    ) {
        Column(
            modifier = Modifier
                .fillMaxSize()
                .padding(it)
                .padding(horizontal = CustomTheme.spaces.large)
                .verticalScroll(rememberScrollState())
        ) {
            ColorSection()
            SpaceSection()
            TypographySection()
        }
    }
}


@Composable
fun ColorSection() {
    Section(title = stringResource(id = R.string.colors)) {
        ColorItem(
            title = stringResource(id = R.string.primary),
            background = CustomTheme.colors.primary
        )
        ColorItem(
            title = stringResource(id = R.string.success),
            background = CustomTheme.colors.success
        )
        ColorItem(
            title = stringResource(id = R.string.error),
            background = CustomTheme.colors.error
        )
    }
}

@Composable
fun Section(modifier: Modifier = Modifier, title: String, content: @Composable () -> Unit) {
    Column(
        modifier = Modifier
            .padding(top = CustomTheme.spaces.extraLarge)
            .then(modifier)
    ) {
        Text(
            text = title.capitalize(),
            style = CustomTheme.typography.h1.copy(color = CustomTheme.colors.text)
        )
        content()
    }
}

@Composable
fun ColorItem(title: String, background: Color) {
    Text(
        text = title,
        textAlign = TextAlign.Center,
        modifier = Modifier
            .padding(top = CustomTheme.spaces.large)
            .background(background)
            .padding(vertical = CustomTheme.spaces.large)
            .fillMaxWidth()
    )
}


@Composable
fun SpaceSection() {
    Section(title = stringResource(id = R.string.spaces)) {
        SpaceItem(
            title = stringResource(id = R.string.small),
            spaceHeight = CustomTheme.spaces.small
        )
        SpaceItem(
            title = stringResource(id = R.string.medium),
            spaceHeight = CustomTheme.spaces.medium
        )
        SpaceItem(
            title = stringResource(id = R.string.large),
            spaceHeight = CustomTheme.spaces.large
        )
        SpaceItem(
            title = stringResource(id = R.string.extralarge),
            spaceHeight = CustomTheme.spaces.extraLarge
        )
    }
}

@Composable
fun SpaceItem(
    modifier: Modifier = Modifier,
    title: String,
    spaceHeight: Dp,
) {
    Column(modifier.padding(top = CustomTheme.spaces.medium)) {
        Text(title.capitalize())
        Box(
            Modifier
                .fillMaxWidth()
                .height(spaceHeight)
                .background(CustomTheme.colors.primary)
        )
    }
}


@Composable
fun TypographySection() {
    Section(title = stringResource(id = R.string.typography)) {
        Text(
            text = stringResource(id = R.string.headline1).capitalize(),
            style = CustomTheme.typography.h1.copy(color = CustomTheme.colors.text)
        )
        Text(
            text = stringResource(id = R.string.body1).capitalize(),
            modifier = Modifier.padding(top = CustomTheme.spaces.medium)
        )
    }
}